package com.longdaji.decoration.ui.register;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/12 0012.
 */

public class RegisterPresenter extends RxPresenter<RegisterContract.IView> implements RegisterContract.IPresenter {
    private DataManager mDataManager;

    @Inject
    public RegisterPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void onGetVerificationCodeClick(final String telephone) {
        if (11 != telephone.length()) {
            mView.showIllegalTelephone();
            return;
        }
        addSubscribe(mDataManager.doTelephoneVerifyApi(null, telephone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (!TextUtils.equals(Constants.OK, httpResponse.getMessage())) {
                            mView.showTelephoneVerifyFailed();
                            return;
                        }
                        Log.d("mytest", "doTelephoneVerifyApi success : " + httpResponse.getMessage());
                        mView.calculateTime();
                        sendVerificationRequest(telephone);
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                    }
                })

        );
    }

    private void sendVerificationRequest(String telephone) {
        addSubscribe(mDataManager.doVerificationRequest(telephone, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("mytest", "sendVerificationRequest success: " + httpResponse.toString());
                        mView.showSendVerification();
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                    }
                }));
    }

    @Override
    public void onConfirmClick(final String telephone, final String password, final String verificationCode) {
        addSubscribe(mDataManager.doVerificationResult(telephone, verificationCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("mytest", "doVerificationResult success: " + httpResponse.toString());
                        mView.showStepTwo();
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                        mView.showCodeError();
                    }
                }));

    }

    @Override
    public void onHidePasswordClick(boolean isPasswordHidden) {
        if (isPasswordHidden) {
            mView.showHidePassword(false);
        } else {
            mView.showHidePassword(true);
        }
    }
}
